let mongoose = require('mongoose'),
    async = require('async'),
    resourcesThumbs = mongoose.model('resources_thumbs'),
    resourcesPost = mongoose.model('resources_posts');

module.exports = (req, res) => {
    let params = req.body;

    if(!params.pid){
        res.json(resFail('pid不能为空！'));
        return false;
    }

    switch(params.type){
        case 'browse':  //浏览次数统计
            resourcesPost.update({
                pid: params.pid
            }, {
                $inc: {
                    browseCount: 1
                }
            }).exec(err => {
                if(err){
                    res.json(resFail(err))
                }else{
                    res.json({
                        result: 1
                    })
                }
            })
            break;
        case 'thumbs':
            async.series([
                // 查询文章数据
                callback => {
                    resourcesPost.findOne({
                        pid: params.pid
                    }, (err, doc) => {
                        if(err){
                            res.json(resFail(err))
                        }else{
                            this.article = doc;
                            callback();
                        }
                    })
                },
                // 查询点赞数据
                callback => {
                    resourcesThumbs.findOne({
                        pid: params.pid
                    }, (err, doc) => {
                        if(err){
                            res.json(resFail(err))
                        }else{
                            this.thumbs = doc;
                            callback();
                        }
                    })
                },
                // 检验是否点赞
                callback => {
                    let list = this.thumbs.thumbsNameList,
                        check = false;

                    try {
                        list.forEach(e => {
                            if(e.mobile == params.mobile){
                                check = true;
                                throw e;
                            }
                        })
                    } catch (err) {}

                    if(check){
                        res.json(resFail('您已经点过赞了！'));
                    }else{
                        callback();
                    }
                },
                // 保存点赞人
                callback => {
                    resourcesThumbs.update({
                        pid: params.pid
                    }, {
                        $push: {
                            thumbsNameList: params.mobile
                        },
                        $inc: {
                            thumbsCount: 1
                        }
                    }).exec(err => {
                        if(err){
                            res.json(resFail(err))
                        }else{
                            callback();
                        }
                    })
                },
                // 更新文章点赞数
                callback => {
                    resourcesPost.update({
                        pid: params.pid
                    }, {
                        $inc: {
                            thumbsCount: 1
                        }
                    }).exec(err => {
                        if(err){
                            res.json(resFail(err))
                        }else{
                            res.json({
                                result: 1
                            });
                        }
                    })
                }
            ])
            break;
        default: break;
    }
}